#include <iostream>
#include <vector>

using namespace std;

struct Node {
    int val,l,r,tot;
};

Node tree[2000000];
int n,ans=1;

int visit(int i) {
    if (i==-1) return 0;
    else {
        tree[i].tot=1+visit(tree[i].l)+visit(tree[i].r);
        return tree[i].tot;
    }
}

bool check(int i,int j) {
    if (i==-1 && j==-1) return true;
    else if ((i==-1)+(j==-1)==1) return false;
    else if (tree[i].val!=tree[j].val) return false;

    int ilc=tree[i].l,irc=tree[i].r;
    int jlc=tree[j].l,jrc=tree[j].r;
    
    return check(ilc,jrc) && check(irc,jlc);
}

int main() {
    cin>>n;
    for (int i=1;i<=n;i++) cin>>tree[i].val;
    for (int i=1;i<=n;i++) cin>>tree[i].l>>tree[i].r;
    visit(1);
    for (int i=1;i<=n;i++)
        if (check(i,i))
            ans=(ans>tree[i].tot) ? ans:tree[i].tot;
    cout<<ans<<endl;
}